import {HTTP} from '../util/http'

class ClassicModel extends HTTP {
    getLatest(callback){
        this.request({
            url: 'classic/latest',
            success:res => {
                callback(res)
                this._setLatestIndex(res.index)
            }
        })
    }
    getClassic(index, nextOrPrevious, callback){
        let key = nextOrPrevious == 'next' ?
            this._getKey(index + 1) : this._getKey(index - 1)
        let classic = wx.getStorageSync(key)
        if(!classic){
            this.request({
                url: `classic/${index}/${nextOrPrevious}`,
                success: (res) => {
                    wx.setStorageSync(this._getKey(res.index),res)
                    callback(res)
                }
            })    
        }else{
            callback(classic)
        }
        
    }
    isFirst(index){
        return index == 1 ? true : false
    }
    isLatest(index){
        return index == this._getLatestIndex() ? true : false
    }
    _setLatestIndex(index){
        wx.setStorageSync('latest', index)
    }
    _getLatestIndex(){
        let index = wx.getStorageSync('latest')
        return index
    }
    _getKey(index){
        return 'classic-' + index
    }
}
export {ClassicModel}